Skip to main content

Data Tables

Data Tables are an easy way to represent data in a structured table. It can be used for all kinds of data types such as localization, items, players,... You can think of it as spreadsheet built into Unity.

DataTable

DataTable

Creating a Data Table

Before we create a Data Table, we first have to create the type that will be stored in the Data Table. To do that, we right click in our project->Create->ExtensionsTools->DataTable->Data Table Type

DataTableType

Now a new script will be created, Let's rename it to ItemDataType. When we open the script we will see something like this.

using ExtensionTools.Data;
public class DataTableType1: DataTable.DataStruct
{
public string ExampleName;
public int ExampleValue;
}

This is just an example, we will rename the class and adjust the class like so:

using ExtensionTools.Data;
public class ItemDataType: DataTable.DataStruct
{
public string ItemName;
public int StackSize;
public Texture ItemIcon;
}

We are now ready to actually create our Data Table. To do that, we right click once again in our project->Create->ExtensionsTools->DataTable->DataTable. And we select the type we just created. DataTableTypeSelect

We have now succesfully created our DataTable and we can start adding data to it, using the + button and clicking on the row we want to edit. DataTableFinished

Accessing rows

There are different ways to access the rows in the DataTable. Let's start with using the keys we can assign in the DataTable.

using ExtensionTools.Data;

...

[SerializeField]
DataTable m_ItemDataTable;

void Start(){
ItemDataType itemData;
if (m_ItemDataTable.TryGetRow<ItemDataType>("key0", out itemData))
{
//Found row
}
else
//No row found with key keyName
}

In our example using keys doesn't really make sense. We rather access these values using the index. This can be done like this.

ItemDataType itemData= m_ItemDataTable.GetRowFromIndex<ItemDataType>(0);